package com.imcode;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 一次读取一个字节数组
 */
public class InputStreamDemo03 {
    public static void main(String[] args) {
        InputStream in = null;
        try {
            //1.创建一个 InputStream的子类对象，FileInputStream 按字节读取文件
            in = new FileInputStream(new File("D:\\test.txt"));
            //2.创建一个字节数组
            byte[] bytes = new byte[4];//1KB 数组的长度用计算机存储单位的整数倍

            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                String str = new String(bytes, 0, len);
                System.out.println(str + ".");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
